CSharpTest.Net
TryGetTarget Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Utils Namespace > WeakReference<T> Class : TryGetTarget Method

value

Glossary Item Box

Returns true if the Object was retrieved.

Syntax

Visual Basic (Declaration) 
Public Function TryGetTarget( _
   ByRef value As T _
) As Boolean
C# 
public bool TryGetTarget( 
   out T value
)

Parameters

value

Example

Library/Library.Test/TestWeakReferenceT.cs

C#Copy Code
string value1 = "Testing Value - 1";
object value2 = new MyObject();
Utils.WeakReference<string> r = new Utils.WeakReference<string>(value1);

string tmp;
Assert.IsTrue(r.TryGetTarget(out tmp) && tmp == value1);

((WeakReference)r).Target = value2; //incorrect type...
Assert.IsFalse(r.IsAlive);
Assert.IsNull(r.Target);
Assert.IsFalse(r.TryGetTarget(out tmp));

Assert.IsTrue(ReferenceEquals(value2, ((WeakReference)r).Target));
VB.NETCopy Code
Dim value1 As String = "Testing Value - 1"
Dim value2 As Object = New MyObject()
Dim r As New Utils.WeakReference(Of String)(value1)

Dim tmp As String
Assert.IsTrue(r.TryGetTarget(tmp) AndAlso tmp = value1)

(DirectCast(r, WeakReference)).Target = value2
'incorrect type...
Assert.IsFalse(r.IsAlive)
Assert.IsNull(r.Target)
Assert.IsFalse(r.TryGetTarget(tmp))

Assert.IsTrue(ReferenceEquals(value2, (DirectCast(r, WeakReference)).Target))

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys